<?php
class system_concept_row implements ArrayAccess {
	protected $_concept;
	public $_data = array();
	public $_updatedData = array();
	
	public function __construct(array $data, system_concept_abstract &$concept) {
		$this->_concept = $concept;
		unset($concept);
		$this->_data = $data;
		
		foreach($this->_concept->getDefaultValues() as $k => $v) {
			$this->set($k, $v);
		}
	}
	
	public function set($name, $val) {		
		$real_name = $this->_getColKey($name);		
		if(null===$real_name) {
			throw new system_exception('can not set '.$name);
		}
		//$this->_data[$name] = $val;	

		if(!array_key_exists($real_name, $this->_updatedData)&&((empty($val)&&$this->_data[$real_name]!==$val)||(!empty($val)&&$this->_data[$real_name]!=$val))) {			
			$this->_updatedData[$real_name] = $val;		
		} elseif(array_key_exists($real_name, $this->_updatedData)) {			
			if((empty($val)&&$this->_data[$real_name]!==$val)||(!empty($val)&&$this->_data[$real_name]!=$val)) {				
				$this->_updatedData[$real_name] = $val;
			} else {
				unset($this->_updatedData[$real_name]);
			}
		}
		return $this;
	}
	/**
	 * 导入数据
	 * @param array $array
	 */
	public function import(array $array) {
		foreach($array as $k => $v) {
			//echo $k.'='.
			if(array_key_exists($k, $this->_data)) {
				$this->set($k, $v);
			}
		}
		return $this;
	}
	/**
	 * 检测是否更新
	 * @param unknown_type $name
	 */
	public function isUpdated($name = null) {
		if($name==null) {
			return !empty($this->_updatedData);
		} else {
			$real_name = $this->_getColKey($name);
			return array_key_exists($real_name, $this->_updatedData);
		}
	}
	public function get($name) {
		$real_name = $this->_getColKey($name);
		if(null===$real_name) {
			throw new system_exception('can not get :'.$name);
		}
		$data = $this->_updatedData+$this->_data;
		return $data[$real_name];
	}
	
	/**
	 * 取得旧的数据值
	 * @param unknown_type $name
	 */
	public function getOrg($name) {
		$real_name = $this->_getColKey($name);
		if(null==$real_name) {
			throw new system_exception('can not get :'.$name);
		}
		return $this->_data[$real_name];
	}
	
	public function __get($name) {
		return $this->get($name);
	}
	public function __set($name, $value) {
		$this->set($name, $value);
	}
	public function __unset($name) {
		throw new system_exception('can not unset the concept data');
	}
	public function __call($method, array $args) {
		if(method_exists($this->_concept, $method)) {
			array_unshift($args, $this);
			return call_user_func_array(array(&$this->_concept, $method), $args);
		}
		$matches = array();
		//get
		if(preg_match('/^get(\w+?)$/', $method, $matches)) {
			if(!!($key = $this->_getColKey($matches[1]))) {
				return $this->get($key);
			}
		}
		//set
		if(preg_match('/^set(\w+?)$/', $method, $matches)) {
			if(!!($key = $this->_getColKey($matches[1]))) {
				$this->set($key, $args[0]);
				return $this;
			}
		}
		throw new system_exception('Can not found the method:'.$method);
	}
	public function _getPrimaryKeyBond($cname) {
		$pk = $this->_concept->getModelsPrimaryKey($cname);
		$where = array();
		$data = $this->_updatedData+$this->_data;
		foreach($pk as $k => $v) {
			$cond = $v.'=?';
			//$pkey = 'pk_'.$cname.'_'.$v;
			if(isset($data[$v])) {
				$where[$cond] = $data[$v];
			}
		}
		return $where;
	}
	
	/**
	 * @return system_db_table
	 * Enter description here ...
	 * @param unknown_type $cname
	 */
	public function getModel($cname) {
		return $this->_concept->getModel($cname);
	}
	
	public function getModelData($modelCanme) {
		$data = $this->_concept->getModelCols($modelCanme);		
		$modelData = array();
		foreach($data as $k => $v) {
			$realName = $this->_concept->getColRealName($k);
			$kcname = $realName[key($realName)];			
			$modelData[$kcname] = isset($this->_updatedData[$k]) ? $this->_updatedData[$k] : $this->_data[$k];		
		}
		return $modelData;
	}
	
	//得到转化后的名子
	public function _getColKey($index) {
		$index = strtolower(str_replace('_', '', $index));
		foreach($this->_data as $key => $value) {
			$keyTmp = strtolower(str_replace('_', '', $key));
			if($keyTmp==$index) {
				return $key;
			}
		}
		return null;
	}
	
	// interface ArrayAccess
	public function offsetExists($index) {
		return isset($this->_data[$index]);
	}
	public function offsetGet($index) {
		return $this->_data[$index];
	}
	public function offsetSet($name, $value) {
		return $this->_data[$name] = $value;
	}
	public function offsetUnset($index) {
		throw new system_exception('can not unset index:'.$index);
	}
	
	public function toArray() {
		$args = func_get_args();
		if($args) {
			$data = array();
			foreach($args as $v) {
				$for = $this->_updatedData+$this->_data;
				if(isset($for[$v])) {
					$data[$v] = $for[$v];
				}
			}
			return $data;
		}
		return (array) $this->_updatedData+$this->_data;
	}
	
	public function __destruct() {

	}
}